<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>数据存储</title>
    <script>
        document.cookie = 'UserName=Paul; expires=Tue, 28 Dec 2020 00:00:00 GMT;';
    </script>
</head>
<body>
<p>This page just created a cookie</p>
<script>
    var now = new Date();
    now.setMonth(now.getMonth() + 6);
    var afterSixMonths = now.toUTCString();
    document.cookie = 'UserName=Paul; expries=' + afterSixMonths + ';';
    document.cookie = 'UserName=Paul; expries=' + afterSixMonths + ';path=/mystore;';
    document.cookie = 'UserName=Paul; expries=' + afterSixMonths + ';path=/;';
    document.cookie = 'UserName=Paul; expries=' + afterSixMonths + ';path=/; domain=mydomain.com;';
    document.cookie = 'UserName=Paul; expries=' + afterSixMonths + ';path=/; domain=mydomain.com; secure=true;';
</script>
<script>
    function setCookie(name, value, expries, path) {
        value = decodeURI(value);
        if (!expries) {
            var now = new Date();
            now.setMonth(now.getMonth() + 6);
            expries = now.toUTCString();
        }
        if (path) {
            path = ';path=' + path;
        }
        document.cookie = name + '=' + value + ';expries=' + expries + path;
    }
</script>
<script>
    alert(decodeURI('2011 a space oydssey;'));
</script>
<script>
    function getCookie(name) {
        var value = document.cookie;
        var cookieStartsAt = value.indexOf(' ' + name + '=');
        if (cookieStartsAt == -1) {
            cookieStartsAt = value.indexOf(name + '=');
        }
        if (cookieStartsAt == -1) {
            value = null;
        } else {
            cookieStartsAt = value.indexOf('=', cookieStartsAt) + 1;
            var cookieEndsAt = value.indexOf(';', cookieStartsAt);
            if (cookieEndsAt == -1) {
                cookieEndsAt = value.length;
            }
            value = encodeURI(value.substring(cookieStartsAt, cookieEndsAt));
        }
        return value;
    }
</script>
<script>
    setCookie('TestCookie', 'Yes');
    if (getCookie('TestCookie')) {
        alert('This website requires cookies to function');
    }

    setCookie('Name', 'Karen');
    setCookie('Age', '44');
    setCookie('LastVisit', '10 Jan 2001');

    setCookie('UserDetails', 'Karen;44;10 Jan 2001');
    var cookieValues = getCookie('UserDetails');
    cookieValues = cookieValues.split(';');
    alert('Name = ' + cookieValues[0]);
    alert('Age = ' + cookieValues[1]);
    alert('Last Visiti = ' + cookieValues[2]);

    localStorage.setItem('userName', 'Paul');
    localStorage.userName = 'Paule';
    //    localStorage.user name = 'Paule'; invalid!
    localStorage.setItem('user name', 'Paul');

    var name = localStorage.getItem('userName');
    name = localStorage.userName;
    //    name=localStorage.getItem('UserName'); null

    localStorage.removeItem('userName');
    localStorage.userName = null;
    localStorage.clear(); // no more kye/value pairs

    localStorage.age = 35;
    var age = localStorage.age;
    alert(typeof age); // string
    age = parseInt(localStorage.age, 10);

    var johnDoe = {
        firstName: 'John',
        lastName: 'Doe',
        age: 35
    };
    localStorage.person = johnDoe;
    var savedPerson = localStorage.person;
    alert(typeof savedPerson); // string
    alert(savedPerson); // [object Object]

    localStorage.person = JSON.stringify(johnDoe);
    savedPerson = JSON.parse(localStorage.person);
</script>
</body>
</html>